// pages/login/login.js
let util = require("../../utils/util");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    password: '',
    openId: '',

  },


  wechatLogin: function (e) {
    const that = this

    var info =
    {
      openId: that.openId
    }
    wx.cloud.callFunction({
      name: 'doPost',
      data: {
        url: app.globalData.url + '/user/userLoginByOpenId',
        info: info
      },
    }).then(res => {
      console.log(res.result)
      if (res.result != null) {
        if (res.result.status == '已锁定') {
          wx.showModal({
            title: "提示",
            content: "你的账号已被锁定!",
            showCancel: false
          })
        }
        else {
          app.globalData.userId = res.result.userId
          app.globalData.name = res.result.name
          app.globalData.token = res.result.token
          app.globalData.phone = res.result.phone
          app.globalData.school = res.result.school
          app.globalData.class = res.result.clas
          wx.switchTab({
            url: '/pages/upLoadRecord/upLoadRecord',
          })
        }
      }
      else {
        wx.showModal({
          title: '提示',
          content: '该微信还未绑定任何账号',
          showCancel: false
        })
      }
    })
  },

  lockWechat: function (e) {
    wx.redirectTo({
      url: '/pages/lockWechat/lockWechat',
    })
  },

  choose: function () {
    wx.chooseLocation({
      success: res => {
        console.log(res)
      }
    })
  },

  idInput: function (e) {
    const that = this
    // console.log(e.detail)
    that.id = e.detail.value
  },

  passwordInput: function (e) {
    const that = this
    that.password = e.detail.value
  },


  // 验证码验证结果回调
  handlerVerify: function (ev) {
    const that = this
    // 如果使用了 mpvue，ev.detail 需要换成 ev.mp.detail
    if (ev.detail.ret === 0) {
      // 验证成功
      that.login()
      // console.log('ticket:', ev.detail.ticket)
    } else {
      // 验证失败
      // 请不要在验证失败中调用refresh，验证码内部会进行相应处理
    }
  },
  // 验证码准备就绪
  handlerReady: function () {

  },
  // 验证码弹框准备关闭
  handlerClose: function () {

  },
  // 验证码出错
  handlerError: function (ev) {
    console.log(ev.detail.errMsg)
  },

  toVeri: function (e) {
    const that = this
    if (util.isNull(that.id)) {
      wx.showModal({
        title: '提示',
        content: '请先输入学号',
        showCancel: false
      })
    }
    else if (util.isNull(that.password)) {
      wx.showModal({
        title: '提示',
        content: '请先输入密码',
        showCancel: false
      })
    }
    else {
      that.selectComponent('#captcha').show()
    }

  },

  login: function (e) {
    const that = this
    var info =
    {
      userId: that.id,
      password: that.password
    }
    wx.cloud.callFunction({
      name: 'doPost',
      data: {
        url: app.globalData.url + '/user/userLogin',
        info: info
      },
    }).then(res => {
      console.log(res.result)
      if (res.result != null) {
        if (res.result.status == '已锁定') {
          wx.showModal({
            title: "提示",
            content: "你的账号已被锁定!",
            showCancel: false
          })
        }
        else {
          app.globalData.userId = res.result.userId
          app.globalData.name = res.result.name
          app.globalData.token = res.result.token
          app.globalData.phone = res.result.phone
          app.globalData.school = res.result.school
          app.globalData.class = res.result.clas

          wx.switchTab({
            url: '/pages/upLoadRecord/upLoadRecord',
          })
        }

      } else {
        wx.showModal({
          title: '提示',
          content: '学号或密码不正确!',
          showCancel: false
        })
      }
    })






  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        getApp().globalData.lati = res.latitude
        getApp().globalData.long = res.longitude
      },
    });
    wx.cloud.callFunction({
      name: 'getOpenId',
    }).then(res => {
      that.openId = res.result.openid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})